<?php

namespace app\admin\controller\paotui;

use addons\paotui\service\Config;
use app\common\controller\Backend;

/**
 * 佣金比例管理
 *
 * @icon fa fa-circle-o
 */
class Brokeragescale extends Backend
{
    
    /**
     * Distanceprice模型对象
     * @var \app\admin\model\paotui\Brokeragescale
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\paotui\Brokeragescale;

    }

    public function index()
    {
        if($this->request->isPost()){
            $params = $this->request->post("row/a");

            $min_extract_money = $params['min_extract_money'];
            $fee_scale = $params['fee_scale'];
            $pay_type = $params['pay_type'];
            $bprice_scale = $params['bprice_scale'];

            Config::set('min_extract_money',$min_extract_money);
            Config::set('fee_scale',$fee_scale);
            Config::set('pay_type',$pay_type);
            Config::set('bprice_scale',$bprice_scale);

            if($params['scale']){
                foreach ($params['scale'] as $k=>$v){
                    $model = new \app\admin\model\paotui\Brokeragescale();
                    $price = $model->where(['type'=>$k])->find();
                    if($price){
                        $price->save(['scale'=>$v]);
                    }
                }
            }
            $this->success("请求成功");
        }

        $model = new \app\admin\model\paotui\Brokeragescale();
        $scaleList = collection($model
            ->alias("scale")
            ->field("scale.*,service.type_name")
            ->join("paotui_service service","scale.type=service.id")
            ->order("type asc")->select())->toArray();

        $this->assign('scaleList',$scaleList);
        $this->assign('min_extract_money',Config::get('min_extract_money'));
        $this->assign('fee_scale',Config::get('fee_scale'));
        $this->assign('pay_type',Config::get('pay_type'));
        $this->assign('bprice_scale',Config::get('bprice_scale'));

        return $this->fetch();
    }
}
